package com.ctgu.fcftsystem.controller;

import com.ctgu.fcftsystem.pojo.Income;
import com.ctgu.fcftsystem.pojo.Securities;
import com.ctgu.fcftsystem.pojo.User;
import com.ctgu.fcftsystem.service.IncomeService;
import com.ctgu.fcftsystem.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/IncomeController")
public class IncomeController {

    @Autowired
    private IncomeService incomeService;

    @Autowired
    private UserService userService;

    @RequestMapping("/findByPaging")
    public Object findByPaging(int pageCount,int pageSize){
        ModelMap map = new ModelMap();
        PageHelper.startPage(pageCount, pageSize);//设置数据库分页查询的范围
        try {
            List<Income> userList = incomeService.findByPaging(pageCount, pageSize);
            //分页的信息
            PageInfo<Income> pageInfo=new PageInfo<>(userList);
            map.put("msg","查询所有角色成功");
            map.put("data",pageInfo);
            map.put("code",0);
        }catch (Exception e){
            e.getStackTrace();
            map.put("msg","查询所有角色失败");
            map.put("code",1);
        }
        return map;
    }

    @RequestMapping("/addIncome")
    public Map addIncome(Integer inum, Integer itype, String iwho, String ipath, String iremark){
        Map<String,Object> map = new HashMap<>();
        if(inum!=null&&itype!=null&&iwho!=null){
            Income income = new Income();

            income.setIncomeNum(inum);
            income.setIncomeType(itype);
            income.setIncomeWho(iwho);
            income.setIncomeDate(new Date());
            income.setIncomePath(ipath);
            income.setIncomeRemark(iremark);

            User beforeUser = userService.getUserMoneyById(iwho);
            int beforeMoney = beforeUser.getUserMoney();
            int afterMoney = beforeMoney + inum;
            int res1 = userService.setUserMoneyById(iwho,afterMoney);

            int res = incomeService.addIncome(income);
            if(res>0){
                //添加成功
                map.put("code",0);
                map.put("msg","添加成功");
                map.put("data",res);
            }else{
                //添加失败
                map.put("code",1);
                map.put("msg","添加失败");
            }
        }else{
            //信息不完整
            map.put("code",1);
            map.put("msg","请填写完整信息");
        }
        return map;
    }

    @RequestMapping("/getIncomeList")
    public Map getIncomeList(){
        Map<String,Object> map = new HashMap<>();
        List<Income> list = incomeService.getIncomeList();
        if(list!=null&&!list.isEmpty()){
            //查询到所有收入记录
            map.put("code",0);
            map.put("msg","所有收入记录如下");
            map.put("data",list);
        }else{
            //未查到任何收入记录
            map.put("code",1);
            map.put("msg","没有任何收入记录");
        }
        return map;
    }

    @RequestMapping("/delIncomeById")
    public Map delIncomeById(int iid){
        Map<String,Object> map = new HashMap<>();
        int res = incomeService.delIncomeById(iid);
        if(res == 1){
            //删除成功
            map.put("code",0);
            map.put("msg","删除成功");
            map.put("data",res);
        }else{
            //删除失败
            map.put("code",1);
            map.put("msg","删除失败");
        }
        return map;
    }


    @RequestMapping("/updateIncome")
    public Map updateIncome(Integer iid,Integer inum, Integer itype, String iwho, Date idate,String ipath, String iremark){
        Map<String,Object> map = new HashMap<>();
        Income income = new Income();

        income.setIncomeId(iid!=null?iid:iid);
        income.setIncomeNum(inum!=null?inum:-1);
        income.setIncomeType(itype!=null?itype:-1);
        income.setIncomeWho(iwho!=null?iwho:iwho);
        income.setIncomeDate(idate!=null?idate:idate);
        income.setIncomePath(ipath!=null?ipath:ipath);
        income.setIncomeRemark(iremark!=null?iremark:iremark);

        int res = incomeService.updateIncome(income);
        if(res>0){
            //修改成功
            map.put("code",0);
            map.put("msg","修改成功");
            map.put("data",res);
        }else{
            //修改失败
            map.put("code",1);
            map.put("msg","修改失败");
        }
        return map;
    }

    @RequestMapping("/searchIncome")
    public Map searchIncome(Integer iid,Integer inum, Integer itype, String iwho, Date idate,String ipath, String iremark){
        Map<String,Object> map = new HashMap<>();
        Income income = new Income();

        income.setIncomeId(iid!=null?iid:-1);
        income.setIncomeNum(inum!=null?inum:-1);
        income.setIncomeType(itype!=null?itype:-1);
        income.setIncomeWho(iwho!=null?iwho:iwho);
        income.setIncomeDate(idate!=null?idate:idate);
        income.setIncomePath(ipath!=null?ipath:ipath);
        income.setIncomeRemark(iremark!=null?iremark:iremark);

        List<Income> list = incomeService.searchIncome(income);
        if(list!=null&&!list.isEmpty()){
            //查询收入记录如下
            map.put("code",0);
            map.put("msg","查到收入记录如下");
            map.put("data",list);
        }else{
            //未查到任何收入记录
            map.put("code",1);
            map.put("msg","没用符合条件的收入记录");
        }
        return map;
    }
}
